package listbyorder.access001_100.test2;

import listbyorder.utils.ListNode;

/**
 * @author code_yc
 * @version 1.0
 * @date 2020/6/6 12:57
 */
public class Solution1 {

    // 两数之和
    public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
        if (l1 == null) return l2;
        if (l2 == null) return l1;
        ListNode dummy = new ListNode(-1);
        ListNode temp = dummy;
        int carry = 0;   // 判断是否有进位
        int cur = 0; // 收集当前的累加值
        while (l1 != null && l2 != null) {
            cur = l1.val + l2.val + carry;
            carry = cur / 10;
            cur = cur % 10;
            temp.next = new ListNode(cur);
            temp = temp.next;
            l1 = l1.next;
            l2 = l2.next;
        }
        while (l1 != null) {
            cur = l1.val + carry;
            carry = cur / 10;
            cur = cur % 10;
            temp.next = new ListNode(cur);
            temp = temp.next;
            l1 = l1.next;
        }
        while (l2 != null) {
            cur = l2.val + carry;
            carry = cur / 10;
            cur = cur % 10;
            temp.next = new ListNode(cur);
            temp = temp.next;
            l2 = l2.next;
        }
        if (carry == 1) {
            temp.next = new ListNode(1);
        }
        return dummy.next;
    }
}
